<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="国家" prop="country">
        <el-input
          v-model="queryParams.country"
          placeholder="请输入国家"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="参赛时间" prop="entryTime">
        <el-date-picker
          clearable
          v-model="queryParams.entryTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择参赛时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small " @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="small " @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="small"
          @click="handleAdd"
          v-hasPermi="['system:country:add']"
        >新增参与国家</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['system:country:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['system:country:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['system:country:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    </el-row>

    <el-table  border v-loading="loading" :data="countryList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="国家" align="center" prop="country" />
      <el-table-column label="参赛时间" align="center" prop="entryTime" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.entryTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="参赛人数" align="center" prop="entryNumber" />
      <el-table-column
        label="参与项目" align="center" prop="pPationProject">
        <template slot-scope="scope">
          <span if="scope.row.pPationProject ==1 ? '2022冬奥会':''"></span>
        </template>
        <template slot-scope="scope">
         {{scope.row.pPationProject == 1 ? '2022冬奥运动会项目':''}}
        </template>
      </el-table-column>
      <el-table-column
        label="参与状态" align="center" prop="particiPationStatus"
        :filters="[{ text: '启用', value: 0 }, { text: '禁用', value: 1}]"
        :filter-method="filterTag"
        filter-placement="bottom-end">
        <template slot-scope="scope">
          <el-tag
            :type="scope.row.particiPationStatus === 0 ? 'primary' : 'danger'"
            disable-transitions> {{scope.row.particiPationStatus == 0 ? '启用' :'禁用'}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="参与方式" align="center" prop="particiPationWay" >
        <template slot-scope="scope">
          {{scope.row.particiPationWay == 1 ? '飞机': scope.row.particiPationWay == 2 ? '轮船':'其他交通工具'}}
        </template>
      </el-table-column>
<!--      ：1-飞机，2-轮船，3-其他交通-->
      <el-table-column label="行程安排" align="center" prop="schedule" />
      <el-table-column label="录入时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200px" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="small"
            type="primary"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:country:edit']"
          >修改</el-button>
          <el-button
            size="small"
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:country:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改参赛国家信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
<!--        <el-form-item label="上传国徽" prop="dialogImageUrl">-->
<!--          {{dialogImageUrl}}-->
<!--        <el-upload-->
<!--          action="https://jsonplaceholder.typicode.com/posts/"-->
<!--          list-type="picture-card"-->
<!--          :on-remove="handleRemove"-->
<!--          :on-preview="handlePictureCardPreview"-->
<!--         >-->
<!--          <i class="el-icon-plus"></i>-->
<!--        </el-upload>-->
<!--        <el-dialog :visible.sync="dialogVisible">-->
<!--          <img width="100%" :src="dialogImageUrl" alt="">-->
<!--        </el-dialog>-->
<!--        </el-form-item>-->
        <el-form-item label="国家" prop="country">
          <el-input v-model="form.country" placeholder="请输入国家" style="width: 220px"/>
        </el-form-item>
        <el-form-item label="参赛时间" prop="entryTime">
          <el-date-picker clearable
                          v-model="form.entryTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择参赛时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="参赛人数" prop="entryNumber">
          <el-input v-model="form.entryNumber" placeholder="请输入参赛人数" style="width: 220px"/>
        </el-form-item>
        <el-form-item label="参与项目" prop="pPationProject">
<!--          <el-input v-model="form.pPationProject" placeholder="请输入参与项目" />-->
<!--          ：1-2022东奥运动会项目-->
          <el-select v-model="form.pPationProject" placeholder="请选择参与项目">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="参与状态">
          <el-radio v-model="form.particiPationStatus" :label='0' >启用</el-radio>
          <el-radio v-model="form.particiPationStatus" :label='1' >禁用</el-radio>
        </el-form-item>
        <el-form-item label="参与方式" prop="particiPationWay">
          <el-select v-model="form.particiPationWay" placeholder="请选择参与方式">
            <el-option
              v-for="item in particiPationWayList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
<!--          <el-input v-model="form.schedule" placeholder="请输入行程安排" />-->
        </el-form-item>
        <el-form-item label="行程安排" prop="schedule">
<!--          <el-input v-model="form.schedule" placeholder="请输入行程安排" />-->
          <el-input
            type="textarea"
            maxlength="100"
            show-word-limit
            :autosize="{ minRows: 2, maxRows: 4}"
            placeholder="请输入行程安排"
            v-model="form.schedule">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCountry, getCountry, delCountry, addCountry, updateCountry } from "@/api/system/country";

export default {
  name: "Country",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      dialogImageUrl: '',
      dialogVisible: false,
      options:[
        {
          value:1,
          label:"2022冬奥运动会项目"
        }
      ],
      particiPationWayList:[
        {
          value:1,
          label:"飞机"
        },
        {
          value:2,
          label:"轮船"
        },
        {
          value:3,
          label:"其他交通"
        }
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 参赛国家信息表格数据
      countryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        country: null,
        entryTime: null,
        particiPationWay: null,
      },
      // 表单参数
      form: {
        id: null,
        country: null,
        entryTime: null,
        entryNumber: null,
        pPationProject: null,
        particiPationStatus:0,
        particiPationWay: null,
        schedule: null
      },
      // 表单校验
      rules: {
        country: [
          { required: true, message: "国家不能为空", trigger: "blur" }
        ],
        entryTime: [
          { required: true, message: "参赛时间不能为空", trigger: "blur" }
        ],
        particiPationStatus: [
          { required: true, message: "参与状态不能为空", trigger: "blur" }
        ],
        entryNumber: [
          { required: true, message: "参赛人数不能为空", trigger: "blur" }
        ],
        dialogImageUrl: [
          { required: true, message: "图片不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    filterTag(value,row){
      return row.particiPationStatus === value;
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    /** 查询参赛国家信息列表 */
    getList() {
      this.loading = true;
      listCountry(this.queryParams).then(response => {
        this.countryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        country: null,
        entryTime: null,
        entryNumber: null,
        pPationProject: null,
        particiPationStatus: 0,
        particiPationWay: null,
        schedule: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加参赛国家信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      debugger
      const self =this
      this.reset();
      const id = row.id || this.ids
      getCountry(id).then(response => {
        self.form = response.data;
        self.form.particiPationStatus = row.particiPationStatus;
        self.open = true;
        self.title = "修改参赛国家信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCountry(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCountry(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除参赛国家信息编号为"' + ids + '"的数据项？').then(function() {
        return delCountry(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/country/export', {
        ...this.queryParams
      }, `country_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang="scss" scoped>
.c-red{
  color:#ff4949;
}
.c-green{
  color: #13ce66;
}
</style>
